<?php
/**
 * PHP API for Sendega.no SMS Gateway.
 *
 * @author Teis Lindemark
 */
class sms {
    private $username = null;
    private $password = null;
    private $outgoing = null;
    private $sender = null;
    private $destination = null;
    private $pricegroup = null;
    private $contentTypeID = null;
    private $content = null;
    private $dlrUrl = null;
    private $sendDate = null;
    private $priority = null;
    private $contentHeader = null;
    private $ageLimit = null;
    private $extID = null;
    private $refID = null;
    private $gwID = null;
    private $pid = null;
    private $dcs = null;

    /**
     *
     * @param type $username
     * Username for sending SMS from Sendega.no.
     * @param type $password
     * Password for sending SMS from Sendega.no.
     * @param type $outgoing
     * URL that is used to send the SMS though the gateway, POST and GET can be used.
     * @param type $sender
     * The sender of the text message.
     * @param type $contentTypeID
     * Type of message to send. Default is 1 for bulk SMS.
     * 0 = WAP bookmark
     * 1 = SMS bulk
     * 2 = SMS Premium / CPA
     * 3 = MMS bulk
     * 4 = MMS Premium / CPA
     * 5 = SMS Premium / GAS
     * @param type $dlrUrl
     * URL used to recive delivery report. For bulk sending, ignore this.
     * @param type $priority 
     * -1 = Low priority (Use for bulk sending)
     * 0 = Normal priority
     * 1 = High priority
     */
    function __construct($username, $password,$sender) {
        $this->username = $username;
        $this->password = $password;
        $this->outgoing = 'https://smsc.sendega.com/content.asmx/Send';
        $this->sender = $sender;
        $this->contentTypeID = 1;
        $this->dlrUrl = 'http://jobb.teilin.net/test.php';
        $this->priority = -1;
        $this->contentHeader = null;
        $this->ageLimit = 0;
        $this->extID = null;
        $this->refID = null;
        $this->dcs = 0;
        $this->pid = 0;
        $this->gwID = 0;
    }
    
    /**
     *
     * @param type $destination
     * The destination number. For example 4792082839.
     * @param type $content
     * Message content. For normal SMS, one message is 160 characters,
     * will split up automatically if it is larger.
     * @param type $pricegroup
     * The cost of getting the SMS. Default is 0.
     * @param type $sendDate 
     * Sending date, if it should be send with deley.
     * Date format is: YYYY-MM-DD HH:MM
     */
    function sendSMS($destination,$content, $pricegroup = 0, $sendDate = null) {
        $this->destination = $destination;
        $this->content = $content;
        $this->pricegroup = $pricegroup;
        $this->sendDate = $sendDate;
        
        $var = '?username=' . $this->username;
        $var .= '&password=' . $this->password;
        $var .= '&sender=' . $this->sender;
        $var .= '&destination=' . $this->destination;
        $var .= '&content=' . $this->content;
        $var .= '&priority=' . $this->priority;
        $var .= '&ageLimit=' . $this->ageLimit;
        $var .= '&contentTypeID=' . $this->contentTypeID;
        $var .= '&gwID=' . $this->gwID;
        $var .= '&pid=' . $this->pid;
        $var .= '&dcs=' . $this->dcs;
        $var .= '&dlrUrl=' . $this->dlrUrl;
        $var .= '&pricegroup=' . $this->pricegroup;
        $var .= '&contentHeader=' . $this->contentHeader;
        $var .= '&extID=' . $this->extID;
        $var .= '&refID=' . $this->refID;
        $var .= '&sendDate=' . $this->sendDate;
        /*
        $ch = curl_init($this->outgoing);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $var);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER  ,true);
        $result = curl_exec($ch);
        */
        /*curl_close($ch);
        
        return $result;*/
        
        $serverResult = @file_get_contents($this->outgoing, $var);
        
        $xmlDocument = simplexml_load_string($serverResult);

        if( $xmlDocument->SendResult->Success == 'True' )
            return "Message was sent. Message id: ".$xmlDocument->SendResult->MessageID;
        else
            return "Message was not sent. Errorcode: ".$xmlDocument->SendResult->ErrorNumber.", ErrorMessage: ".$xmlDocument->SendResult->ErrorMessage;
    }
}

?>
